package com.ksd.quartz.service;

import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.enums.SourceTypeEnum;
import com.ksd.common.excel.MovieExcel;
import com.ksd.common.tool.ChinesePinyinConverter;
import com.ksd.common.tool.FileMoveUsingIO;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.*;
import com.ksd.system.service.ISysBoardService;
import com.ksd.system.service.ISysMovieService;
import com.ksd.system.service.ISysTvDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class MovieExcelService {
    private static final Logger logger = LoggerFactory.getLogger(MovieExcelService.class);

    @Autowired
    private ISysMovieService sysMovieService;
    @Autowired
    private ISysTvDataService sysTvDataService;


//    public void insertReadExcelByRootPathSingle(String fileRootPath,MovieExcel movieExcel,String taskId,Integer sourceType){
//        logger.info("待导入文件的根目录:"+fileRootPath+"单独导入电影,数据源:"+sourceType);
//
//        SysTvData sysTvData = new SysTvData();
//        sysTvData.setName(movieExcel.getFileType());
//        sysTvData.setTypeId(Long.valueOf(SysTvType.movieDefaultId));
//        SysTvData sysTvDataResult = sysTvDataService.selectSysTvDataList(sysTvData).get(0);
//
//        SysMovie sysMovie = new SysMovie();
//        sysMovie.setTitle(movieExcel.getTitle());
//        sysMovie.setTypeData(sysTvDataResult.getId());
//        List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
//        Boolean movieFlag = false;
//        if (movieList.size() > 0) {
//            sysMovie = movieList.get(0);
//            movieFlag = true;
//        }
//        String videoAddress = movieExcel.getVideoAddress();
//        File videoFile = new File(fileRootPath+File.separator +videoAddress);
//        if (videoFile.exists()) {
//            sysMovie.setVideoAddress(videoFile.getAbsolutePath());
//            if (sourceType.equals(SourceTypeEnum.init.getCode())){
//                String  substring = sysMovie.getVideoAddress().substring(PlayControlConfig.getNginxRootPath().length());
//                sysMovie.setFileUrl(PlayControlConfig.getMappingPath()+substring);
//            }else{
//                String substring = sysMovie.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
//                sysMovie.setFileUrl(PlayControlConfig.getDiskMappingPath()+substring);
//            }
//        }else{
//            return;
//        }
//        String imgAddress = movieExcel.getImgAddress();
//        File imgFile = new File(fileRootPath+File.separator +imgAddress);
//        if (imgFile.exists()) {
//            sysMovie.setImgAddress(imgFile.getAbsolutePath());
//            if (sourceType.equals(SourceTypeEnum.init.getCode())){
//                String substring = sysMovie.getImgAddress().substring(PlayControlConfig.getNginxRootPath().length());
//                sysMovie.setImgUrl(PlayControlConfig.getMappingPath()+substring);
//            }else{
//                String substring = sysMovie.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
//                sysMovie.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);
//            }
//
//        }else{
//            return;
//        }
//        String keyWord = ChinesePinyinConverter.convertToPinyin(movieExcel.getTitle()).replaceAll("\\s+", "");
//        sysMovie.setKeyWord(keyWord);
//        sysMovie.setTaskId(taskId);
//        sysMovie.setSouceType(sourceType);
//        if (movieFlag) {
//            logger.info("更新电影:"+movieExcel.getTitle()+movieExcel.getVideoAddress()+"^^^^^^^^^^"+taskId+"数据源类型:"+sourceType);
//            sysMovieService.updateSysMovie(sysMovie);
//        }else{
//            logger.info("新增电影:"+movieExcel.getTitle()+"^^^^^^^^^^"+taskId+"数据源类型:"+sourceType);
//            sysMovieService.insertSysMovie(sysMovie);
//        }
//    }
    public void insertReadExcelByRootPath(String fileRootPath,List<MovieExcel> movieExcels,String taskId,Integer sourceType){
        logger.info("待导入文件的根目录:"+fileRootPath+"待导入的电影个数:"+movieExcels.size());

        for (MovieExcel movieExcel : movieExcels) {

            SysTvData sysTvData = new SysTvData();
            sysTvData.setName(movieExcel.getFileType());
            sysTvData.setTypeId(Long.valueOf(SysTvType.movieDefaultId));
            SysTvData sysTvDataResult = sysTvDataService.selectSysTvDataList(sysTvData).get(0);

            SysMovie sysMovie = new SysMovie();
            sysMovie.setTitle(movieExcel.getTitle());
            sysMovie.setTypeData(sysTvDataResult.getId());
            List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
            Boolean movieFlag = false;
            if (movieList.size() > 0) {
                sysMovie = movieList.get(0);
                movieFlag = true;
            }
            String videoAddress = movieExcel.getVideoAddress();
            File videoFile = new File(fileRootPath+File.separator +videoAddress);
            if (videoFile.exists()) {
                sysMovie.setVideoAddress(videoFile.getAbsolutePath());
                if (sourceType.equals(SourceTypeEnum.init.getCode())){
                    String  substring = sysMovie.getVideoAddress().substring(PlayControlConfig.getNginxRootPath().length());
                    sysMovie.setFileUrl(PlayControlConfig.getMappingPath()+substring);
                }else{
                    String substring = sysMovie.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
                    sysMovie.setFileUrl(PlayControlConfig.getDiskMappingPath()+substring);
                }


            }else{
                continue;
            }
            String imgAddress = movieExcel.getImgAddress();
            File imgFile = new File(fileRootPath+File.separator +imgAddress);
            if (imgFile.exists()) {
                sysMovie.setImgAddress(imgFile.getAbsolutePath());
                if (sourceType.equals(SourceTypeEnum.init.getCode())){
                    String substring = sysMovie.getImgAddress().substring(PlayControlConfig.getNginxRootPath().length());
                    sysMovie.setImgUrl(PlayControlConfig.getMappingPath()+substring);
                }else{
                    String substring = sysMovie.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
                    sysMovie.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);
                }

            }else{
                continue;
            }
            String keyWord = ChinesePinyinConverter.convertToPinyin(movieExcel.getTitle()).replaceAll("\\s+", "");
            sysMovie.setKeyWord(keyWord);
            sysMovie.setTaskId(taskId);
            sysMovie.setSouceType(sourceType);
            if (movieFlag) {
                logger.info("更新电影:"+movieExcel.getTitle()+movieExcel.getVideoAddress()+"^^^^^^^^^^"+taskId);
                sysMovieService.updateSysMovie(sysMovie);
            }else{
                logger.info("新增电影:"+movieExcel.getTitle()+"^^^^^^^^^^"+taskId);
                sysMovieService.insertSysMovie(sysMovie);
            }
        }
    }


    //将USB内的内容文件复制到硬盘内,然后再进行数据入库
    public void copyUsbInsertByDirPath(String usbDirPath,MovieExcel movieExcel,String taskId){
        logger.info("检测到服务器插入的硬盘中待导入的电影信息:"+movieExcel.toString());

        SysTvData sysTvData = new SysTvData();
        sysTvData.setName(movieExcel.getFileType());
        sysTvData.setTypeId(Long.valueOf(SysTvType.movieDefaultId));
        List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
        if (StringUtils.isNotNull(sysTvDataList)){
            sysTvData = sysTvDataList.get(0);
        }else{
            sysTvDataService.insertSysTvData(sysTvData);
        }


        SysMovie sysMovie = new SysMovie();
        sysMovie.setTitle(movieExcel.getTitle());
        sysMovie.setTypeData(sysTvData.getId());
        List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
        Boolean movieFlag = false;
        if (movieList.size() > 0) {
            sysMovie = movieList.get(0);
            movieFlag = true;
        }
        String videoAddress = movieExcel.getVideoAddress();
        File videoFile = new File(usbDirPath +videoAddress);
        if (videoFile.exists()) {
            //将USB上的电影文件复制到硬盘上
            String usbAddress = PlayControlConfig.getHardDiskFilePath()+File.separator+"usbData"+File.separator+videoAddress;
            FileMoveUsingIO.moveFile(videoFile.getAbsolutePath(),usbAddress);

            sysMovie.setVideoAddress(usbAddress);
            String substring = sysMovie.getVideoAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
            sysMovie.setFileUrl(PlayControlConfig.getDiskMappingPath()+substring);
        }else{
            return;
        }
        String imgAddress = movieExcel.getImgAddress();
        File imgFile = new File(usbDirPath +imgAddress);
        if (imgFile.exists()) {
            String usbAddress = PlayControlConfig.getHardDiskFilePath()+File.separator+"usbData"+File.separator+imgAddress;
            FileMoveUsingIO.moveFile(imgFile.getAbsolutePath(),usbAddress);

            sysMovie.setImgAddress(usbAddress);
            String substring = sysMovie.getImgAddress().substring(PlayControlConfig.getDiskNginxRootPath().length());
            sysMovie.setImgUrl(PlayControlConfig.getDiskMappingPath()+substring);
        }else{
            return;
        }
        String keyWord = ChinesePinyinConverter.convertToPinyin(movieExcel.getTitle()).replaceAll("\\s+", "");
        sysMovie.setKeyWord(keyWord);
        sysMovie.setTaskId(taskId);
        sysMovie.setSouceType(SourceTypeEnum.usb.getCode());
        if (movieFlag) {
            logger.info("USB导入更新电影:"+movieExcel.getTitle()+movieExcel.getVideoAddress()+"^^^^^^^^^^"+taskId);
            sysMovieService.updateSysMovie(sysMovie);
        }else{
            logger.info("USB导入新增电影:"+movieExcel.getTitle()+"^^^^^^^^^^"+taskId);
            sysMovieService.insertSysMovie(sysMovie);
        }
    }

}
